<?
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 * Description of DataHora
 *
 * @author Paulo
 */

class Calendar
{
   /*
        representa o primeiro dia da semana Domingo
    */
    var $comecoDia = 0;

    /*
        representa o primeiro mes do ano no caso Janeiro
    */
    var $comecoMes = 1;

    /*
        Essas letras representão os dias da semana que ira apareçer no calendario.
    */
    var $diaNomes = array("D", "S", "T", "Q", "Q", "S", "S");

    /*
        array que represento os nomes de todos os meses que serão exibidos no calendario.
    */
    var $mesNomes = array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho",
                            "Julho", "Augosto", "Setembro", "Outubro", "Novembro", "Dezembro");

    /*
    *  array que representa os dias que cada mes tem no ano....a primeira ocorrencia se refere a janeiro
    */
    var $diaDosMeses = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);




    function Calendar(){}



    function getDiaNomes()
    {
        return $this->diaNomes;
    }


    function setDiaNomes($nomes)
    {
        $this->diaNomes = $nomes;
    }


    function getMesNomes()
    {
        return $this->mesNomes;
    }


    function setMeNomes($nomes)
    {
        $this->mesNomes = $nomes;
    }


    function getComecoDia()
    {
        return $this->comecoDia;
    }


    function setComecoDia($dia)
    {
        $this->comecoDia = $dia;
    }


    function getComecoMes()
    {
        return $this->comecoMes;
    }


    function setComecoMes($mes)
    {
        $this->comecoMes = $mes;
    }


    /*
        Return the URL to link to in order to display a calendar for a given month/year.
        You must override this method if you want to activate the "forward" and "back"
        feature of the calendar.

        Note: If you return an empty string from this function, no navigation link will
        be displayed. This is the default behaviour.

        If the calendar is being displayed in "year" view, $month will be set to zero.
    */
    function getCalendarLink($mes, $ano)
    {
        return "";
    }

    /*
        Return the URL to link to  for a given date.
        You must override this method if you want to activate the date linking
        feature of the calendar.

        Note: If you return an empty string from this function, no navigation link will
        be displayed. This is the default behaviour.
    */
    function getDataLink($dia, $mes, $ano)
    {
        return "";
    }


    /*
        retorna um html com o
    */
    function getMesCorrenteView()
    {
        $d = getdate(time());
        return $this->getMesView($d["mes"], $d["ano"]);
    }


    /*
        Return the HTML for the current year
    */
    function getAnoCorrenteView()
    {
        $d = getdate(time());
        return $this->getAnoView($d["ano"]);
    }


    /*
        Return the HTML for a specified month
    */
    function getMesView($mes, $ano)
    {
        return $this->getMesHTML($mes, $ano);
    }


    /*
        Return the HTML for a specified year
    */
    function getAnoView($ano)
    {
        return $this->getAnoHTML($ano);
    }



    /********************************************************************************

        The rest are private methods. No user-servicable parts inside.

        You shouldn't need to call any of these functions directly.

    *********************************************************************************/


    /*
        Calculate the number of days in a month, taking into account leap years.
    */
    function getDiasNoMes($mes, $ano)
    {
        if ($mes < 1 || $mes > 12)
        {
            return 0;
        }

        $d = $this->diaDosMeses[$mes - 1];

        if ($mes == 2)
        {
            // Check for leap year
            // Forget the 4000 rule, I doubt I'll be around then...

            if ($ano%4 == 0)
            {
                if ($ano%100 == 0)
                {
                    if ($ano%400 == 0)
                    {
                        $d = 29;
                    }
                }
                else
                {
                    $d = 29;
                }
            }
        }

        return $d;
    }


    /*
        retorna o calendario do mes e do ano que for passado.
    */
    private function getMesHTML($m, $y, $mostrarAno = 1)
    {
        $retorno = "";

        $a = $this->ajustarData($m, $y);
        $mes = $a[0];
        $ano = $a[1];

    	$diasNoMes = $this->getDiasNoMes($mes, $ano);
    	$data = getdate(mktime(12, 0, 0, $mes, 1, $ano));

    	$primeiro = $data["wday"];
    	$mesNome = $this->mesNomes[$mes - 1];

    	$anterior = $this->ajustarData($mes - 1, $ano);
    	$proximo = $this->ajustarData($mes + 1, $ano);

    	if ($mostrarAno == 1)
    	{
    	    $mesAnterior = $this->getCalendarLink($anterior[0], $anterior[1]);
    	    $proximoMes = $this->getCalendarLink($proximo[0], $proximo[1]);
    	}
    	else
    	{
    	    $mesAnterior = "";
    	    $proximoMes = "";
    	}

    	$header = $mesNome . (($mostrarAno > 0) ? " " . $ano : "");

    	$retorno .= "<table border='1'>\n";
    	$retorno .= "<tr>\n";
    	$retorno .= "<td>" . (($mesAnterior == "") ? "&nbsp;" : "<a href=\"$mesAnterior\">&lt;&lt;</a>")  . "</td>\n";
    	$retorno .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n";
    	$retorno .= "<td>" . (($proximoMes == "") ? "&nbsp;" : "<a href=\"$proximoMes\">&gt;&gt;</a>")  . "</td>\n";
    	$retorno .= "</tr>\n";

    	$retorno .= "<tr>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia)%7] . "&nbsp;</th>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia+1)%7] . "&nbsp;</th>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia+2)%7] . "&nbsp;</th>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia+3)%7] . "&nbsp;</th>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia+4)%7] . "&nbsp;</th>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia+5)%7] . "&nbsp;</th>\n";
    	$retorno .= "<th>&nbsp;" . $this->diaNomes[($this->comecoDia+6)%7] . "&nbsp;</th>\n";
    	$retorno .= "</tr>\n";

    	// We need to work out what date to start at so that the first appears in the correct column
    	$d = $this->comecoDia + 1 - $primeiro;
    	while ($d > 1)
    	{
    	    $d -= 7;
    	}

        // Make sure we know when today is, so that we can use a different CSS style
        $hoje = getdate(time());

    	while ($d <= $diasNoMes)
    	{
    	    $retorno .= "<tr>\n";

    	    for ($i = 0; $i < 7; $i++)
    	    {
        	    $class = ($ano == $hoje["year"] && $mes == $hoje["mon"] && $d == $hoje["mday"]) ? "calendarToday" : "calendar";
    	        $retorno .= "<td align=\"center\">";
    	        if ($d > 0 && $d <= $diasNoMes)
    	        {
                    if($d == $hoje["mday"]){
                        $link = $this->getDataLink($d, $mes, $ano);
                        $retorno .= (($link == "") ? "<font color='red'><b>$d</b></font>" : "<a href=\"$link\">$d</a>");
                    }else{
                        $link = $this->getDataLink($d, $mes, $ano);
                        $retorno .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
                    }
    	        }
    	        else
    	        {
    	            $retorno .= "&nbsp;";
    	        }
      	        $retorno .= "</td>\n";
        	    $d++;
    	    }
    	    $retorno .= "</tr>\n";
    	}

    	$retorno .= "</table>\n";

    	return $retorno;
    }


    /*
       gera um calendario com todos os meses com ano que for informado.
    */
    function getAnoHTML($ano)
    {
        $retorno = "";
    	$anterior = $this->getCalendarLink(0, $ano - 1);
    	$proximo = $this->getCalendarLink(0, $ano + 1);

        $retorno .= "<table class=\"calendar\" border=\"0\">\n";
        $retorno .= "<tr>";
    	$retorno .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($anterior == "") ? "&nbsp;" : "<a href=\"$anterior\">&lt;&lt;</a>")  . "</td>\n";
        $retorno .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->comecoMes > 1) ? $ano . " - " . ($ano + 1) : $ano) ."</td>\n";
    	$retorno .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($proximo == "") ? "&nbsp;" : "<a href=\"$proximo\">&gt;&gt;</a>")  . "</td>\n";
        $retorno .= "</tr>\n";
        $retorno .= "<tr>";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(0 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(1 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(2 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "</tr>\n";
        $retorno .= "<tr>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(3 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(4 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(5 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "</tr>\n";
        $retorno .= "<tr>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(6 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(7 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(8 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "</tr>\n";
        $retorno .= "<tr>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(9 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(10 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "<td class=\"calendar\" valign=\"top\">" . $this->getMesHTML(11 + $this->comecoMes, $ano, 0) ."</td>\n";
        $retorno .= "</tr>\n";
        $retorno .= "</table>\n";

        return $retorno;
    }

    function ajustarData($mes, $ano)
    {
        $a = array();
        $a[0] = $mes;
        $a[1] = $ano;

        while ($a[0] > 12)
        {
            $a[0] -= 12;
            $a[1]++;
        }

        while ($a[0] <= 0)
        {
            $a[0] += 12;
            $a[1]--;
        }

        return $a;
    }


}

?>
